/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.CourseMealsOrder;
import me.zhengjie.modules.system.domain.CourseOrder;
import me.zhengjie.modules.system.service.dto.CourseOrderQueryCriteria;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.CourseMealsOrderRepository;
import me.zhengjie.modules.system.service.CourseMealsOrderService;
import me.zhengjie.modules.system.service.dto.CourseMealsOrderDto;
import me.zhengjie.modules.system.service.dto.CourseMealsOrderQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.CourseMealsOrderMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author 王宇
* @date 2023-09-21
**/
@Service
@RequiredArgsConstructor
public class CourseMealsOrderServiceImpl implements CourseMealsOrderService {

    private final CourseMealsOrderRepository courseMealsOrderRepository;
    private final CourseMealsOrderMapper courseMealsOrderMapper;

    @Override
    public Map<String,Object> queryAll(CourseMealsOrderQueryCriteria criteria, Pageable pageable){
        Page<CourseMealsOrder> page = courseMealsOrderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(courseMealsOrderMapper::toDto));
    }

    @Override
    public List<CourseMealsOrderDto> queryAll(CourseMealsOrderQueryCriteria criteria){
        return courseMealsOrderMapper.toDto(courseMealsOrderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public CourseMealsOrderDto findById(Long orderId) {
        CourseMealsOrder courseMealsOrder = courseMealsOrderRepository.findById(orderId).orElseGet(CourseMealsOrder::new);
        ValidationUtil.isNull(courseMealsOrder.getOrderId(),"CourseMealsOrder","orderId",orderId);
        return courseMealsOrderMapper.toDto(courseMealsOrder);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CourseMealsOrderDto create(CourseMealsOrder resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setOrderId(snowflake.nextId()); 
        return courseMealsOrderMapper.toDto(courseMealsOrderRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CourseMealsOrder resources) {
        CourseMealsOrder courseMealsOrder = courseMealsOrderRepository.findById(resources.getOrderId()).orElseGet(CourseMealsOrder::new);
        ValidationUtil.isNull( courseMealsOrder.getOrderId(),"CourseMealsOrder","id",resources.getOrderId());
        courseMealsOrder.copy(resources);
        courseMealsOrderRepository.save(courseMealsOrder);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long orderId : ids) {
            courseMealsOrderRepository.deleteById(orderId);
        }
    }

    @Override
    public void download(List<CourseMealsOrderDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CourseMealsOrderDto courseMealsOrder : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("订单编号", courseMealsOrder.getOrderNo());
            map.put("openid", courseMealsOrder.getOpenId());
            map.put("预支付流水号", courseMealsOrder.getPrePayid());
            map.put("支付流水号", courseMealsOrder.getPaySerialid());
            map.put("支付金额", courseMealsOrder.getPayAmount());
            map.put("餐费", courseMealsOrder.getMeals());
            map.put("支付状态，0：未支付，1：已支付", courseMealsOrder.getPayStatus());
            map.put("更新时间", courseMealsOrder.getUpdateTime());
            map.put("创建时间", courseMealsOrder.getCreateTime());
            map.put("备注", courseMealsOrder.getRemark());
            map.put("来源，0：签到缴费", courseMealsOrder.getOrderSource());
            map.put("业务ID", courseMealsOrder.getBizId());
            map.put("班级编号", courseMealsOrder.getClassNo());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public CourseMealsOrder findOne(String orderSn) {
        CourseMealsOrderQueryCriteria criteria = new CourseMealsOrderQueryCriteria();
        criteria.setOrderNo(orderSn);
        List<CourseMealsOrder> all = courseMealsOrderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
        if (CollectionUtils.isEmpty(all)){
            return null;
        }
        return all.get(0);
    }
}